﻿using System.Collections.Generic;
using Helpers;
using QFramework;

namespace Gp.Scripts.Core
{
    /// <summary>
    ///  并不处于开战状态的状态机
    /// </summary>
    public class GameStateNoBattle : GameState
    {
        private List<SkillEntity> _abilities;
        private BaseUnit _playerUnit;
        
        // public AbilityState State { get; private set; }

        protected override void OnStateEnter(GameState lastState)
        {
            // 将玩家的输入更改为 SetTarget 模式
            var playerCharacter = ObjectHelper.PlayerCharacter;
            playerCharacter.characterLocomotion.isControllable = true;
            
            
            // 载入探索界面UI
            Global.Get<GUIManager>().PushPanel(ConstPanelId.BattlePanel);
            
            // 选中玩家 (临时)
            // todo New: 换用统一的DelayCall, 使其能够在退出状态时析构 
            ActionKit.Delay(.2f, () =>
                {
                    _playerUnit = playerCharacter.GetComponent<EntityBehaviour>().Entity as BaseUnit;
                    _playerUnit?.OnEntitySelect();
                })
                .Start(GameEntry.Instance);
            
            // 点击
            RegisterEvent<Event_SkillSelect>(OnSkillSelect);
        }

        
        private void OnSkillSelect(Event_SkillSelect obj)
        {
            var skill = obj.Skill;
            if (!skill.CanSelect().Result()) return;
            
            // 进入技能显示态
            this.SwitchState(new GameStateSkillSelect(skill));
        }
        

        protected override void OnStateExit(GameState nextState)
        {
            var playerCharacter = ObjectHelper.PlayerCharacter;
            playerCharacter.characterLocomotion.isControllable = false;
            playerCharacter.characterLocomotion.Stop();
        }


        public override void OnEntitySelectEvent(BaseEntity entity, SelectEventType eventType)
        {
            base.OnEntitySelectEvent(entity, eventType);
            if (eventType != SelectEventType.SELECT)
            {
                return;
            }
            // 交互性点击
            entity.OnEntitySelect();
            // if (entityObject is not BaseUnit unit) return;
            // 列出所有行动交互（攻击、交谈）
            
        }
    }
}